package com.sangcomz.fishbun.ui.album.adapter;

import com.sangcomz.fishbun.ResourceTable;
import com.sangcomz.fishbun.adapter.image.ImageAdapter;
import com.sangcomz.fishbun.ui.album.model.Album;
import com.sangcomz.fishbun.ui.album.model.AlbumMetaData;
import com.sangcomz.fishbun.ui.album.listener.AlbumClickListener;
import com.sangcomz.fishbun.util.BaseGridSliceProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AlbumListAdapter extends BaseGridSliceProvider<Album, AlbumListAdapter.AlbumViewHolder> {

    private AlbumClickListener albumClickListener;
    private int thumbnailSize;
    private ImageAdapter imageAdapter;
//    private List<Album> albumList = new ArrayList<>();
    private AbilitySlice abilitySlice;

    public AlbumListAdapter(AbilitySlice abilitySlice, AlbumClickListener albumClickListener, int thumbnailSize, ImageAdapter imageAdapter, GridConfig config) {
        this(abilitySlice, new ArrayList<>(), ResourceTable.Layout_album_item, config);
        this.abilitySlice = abilitySlice;
        this.albumClickListener = albumClickListener;
        this.thumbnailSize = thumbnailSize;
        this.imageAdapter = imageAdapter;
    }

    private AlbumListAdapter(Context context, List<Album> itemData, int resId, GridConfig config) {
        super(context, itemData, resId, config);
        setItemClickListener((component, position, indexPosition) -> albumClickListener.onAlbumClick(position, this.itemData.get(position)));
    }

    @Override
    public void onBindViewHolder(AlbumViewHolder viewHolder, Album data, int viewType, int position, int indexPosition) {
        viewHolder.setData(data);
    }

    @Override
    public AlbumViewHolder onCreateViewHolder(Component component, int viewType) {
        return new AlbumViewHolder(component);
    }

    public void setAlbumList(List<Album> albumList) {
//        this.albumList = albumList;
        this.itemData = albumList;
        notifyDataChanged();
    }

    class AlbumViewHolder extends BaseGridSliceProvider.BaseViewHolder {
        private final Image imgAlbumThumb;
        private final Text txtAlbumName;
        private final Text txtAlbumCount;

        AlbumViewHolder(Component component) {
            super(component);
            imgAlbumThumb = (Image) component.findComponentById(ResourceTable.Id_img_album_thumb);
            txtAlbumName = (Text) component.findComponentById(ResourceTable.Id_txt_album_name);
            txtAlbumCount = (Text) component.findComponentById(ResourceTable.Id_txt_album_count);
            if (thumbnailSize > 0) {
                ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(thumbnailSize, thumbnailSize);
                imgAlbumThumb.setLayoutConfig(layoutConfig);
            }
        }

        void setData(Album album) {
            Uri uri = Uri.parse(album.getMetaData().getThumbnailPath());
            imageAdapter.loadImage(abilitySlice,imgAlbumThumb, uri);
            txtAlbumName.setText(album.getDisplayName());
            txtAlbumCount.setText(String.valueOf(album.getMetaData().getCount()));
        }
    }

    public void updateAlbumMeta(int position, int addedCount, String thumbnailPath) {
        if (itemData == null || position >= itemData.size()) {
            return;
        }
        Album oldAlbum = itemData.get(position);
        Album updateAlbum = new Album(
                oldAlbum.getId(),
                oldAlbum.getDisplayName(),
                new AlbumMetaData(oldAlbum.getMetaData().getCount() + addedCount, thumbnailPath));

        itemData.set(position, updateAlbum);
        notifyDataSetItemChanged(position);
    }

}